import { datashopAxios } from '@/utils/request.ts'
import { downloadBlobFile } from '@/api/file.ts'
import { getItem, USERRESULT } from '@hs-admin/utils'

export const getMapList = (params?: { keyword?: string; page?: number; page_size?: number }): Promise<any> => {
  return datashopAxios({
    url: '/api/datashop/v3/spec-option/maps',
    method: 'get',
    params
  })
}

export const getParkList = (params?: { keyword?: string; page?: number; page_size?: number }) => {
  return datashopAxios({
    url: '/api/datashop/v3/spec-option/parks',
    method: 'get',
    params
  })
}

export const getShoppingCartList = (params?: { page?: number; page_size?: number }) => {
  return datashopAxios({
    url: '/api/datashop/v3/cart/items',
    method: 'get',
    params
  })
}

export const postCompanyList = (data: FormData) => {
  return datashopAxios({
    url: '/api/datashop/v3/company-id-spec-vals',
    method: 'post',
    data
  })
}

export const exportFileTemplate = () => {
  return datashopAxios({
    url: '/api/datashop/v3/company-id-spec-vals/file',
    method: 'get',
    responseType: 'arraybuffer'
  }).then((res: any) => {
    downloadBlobFile(res, '模板.xlsx')
  })
}

export const exportWhiteCompanyFile = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/company-id-spec-vals/${id}/file`,
    method: 'get',
    responseType: 'arraybuffer'
  }).then((res: any) => {
    downloadBlobFile(res, '白名单.xlsx')
  })
}

export const getCompanyList = (
  id: string,
  params?: {
    keywords?: string
    page?: number
    page_size?: number
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/company-id-spec-vals/${id}`,
    method: 'get',
    params
  })
}

export const postIndustryField = (data: {
  id_names: Array<{
    id: string
    name: string
  }>
}) => {
  return datashopAxios({
    url: '/api/datashop/v3/industry-spec-vals',
    method: 'post',
    data
  })
}

export const postParkField = (data: {
  id_names: Array<{
    id: string
    name: string
  }>
}) => {
  return datashopAxios({
    url: '/api/datashop/v3/park-spec-vals',
    method: 'post',
    data
  })
}

export const postAreaField = (data: { vals: Array<string> }) => {
  return datashopAxios({
    url: '/api/datashop/v3/pca-spec-vals',
    method: 'post',
    data
  })
}

export const postAggregationField = (data: { specification_id: string; vals: Array<string> }) => {
  return datashopAxios({
    url: '/api/datashop/v3/polymerize-spec-vals',
    method: 'post',
    data
  })
}

export const postTimeField = (data: { specification_id: string; start_time: string; end_time?: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/time-spec-vals',
    method: 'post',
    data
  })
}

export const createCartItem = (data: { product_id: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/cart/items',
    method: 'post',
    data
  })
}

export const getAggrOptions = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/specifications/polymerize-specs/${id}/options`,
    method: 'get'
  })
}

export const deleteShoppingCart = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/cart/items/${id}`,
    method: 'delete'
  })
}

export const deleteDraftOrder = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/data-sync/${id}`,
    method: 'delete'
  })
}

export const getIndustrySpec = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/industry-spec-vals/${id}`,
    method: 'get'
  })
}
export const getParkSpec = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/park-spec-vals/${id}`,
    method: 'get'
  })
}
export const getAreaSpec = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/pca-spec-vals/${id}`,
    method: 'get'
  })
}
export const getPolymerizeSpec = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/polymerize-spec-vals/${id}`,
    method: 'get'
  })
}
export const getTimeSpec = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/time-spec-vals/${id}`,
    method: 'get'
  })
}

export const unbindSepcById = (cart_item_id: string, id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/cart-items/${cart_item_id}/spec-vals/${id}/unbind`,
    method: 'post'
  })
}

export const bindSepcById = (cart_item_id: string, id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/cart-items/${cart_item_id}/spec-vals/${id}/bind`,
    method: 'post'
  })
}

export const getShoppingOrderItems = (params: { order_id: string; page: number; page_size: number }) => {
  return datashopAxios(
    {
      url: '/api/datashop/v3/order-items',
      method: 'get',
      params
    },
    {
      error_message_show: false
    }
  )
}

export const updateOrderItemMapTable = (id: string, data: { mapping_table_name: string }) => {
  return datashopAxios({
    url: `/api/datashop/v3/order-items/${id}`,
    method: 'put',
    data
  })
}

export const getOrderItemDetail = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/order-items/${id}`,
    method: 'get'
  })
}

export const getOrderItemMappings = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v4/data-item-products/${id}/columns`,
    method: 'get',
    params: {
      available: true,
      page: 1,
      page_size: 9999
    }
  })
}

export const getOrderItemMappingsResults = (params: { order_item_id: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/mapping-columns',
    method: 'get',
    params: {
      ...params,
      page: 1,
      page_size: 9999
    }
  })
}

export const getAllShoppingOrderItems = (order_id: string) => {
  return getShoppingOrderItems({
    order_id,
    page: 1,
    page_size: 9999
  })
}

export const getDstTables = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${id}/tables`,
    method: 'get'
  })
}

export const putDraftOrderItem = (
  id: string,
  order_id: string,
  data: {
    dst_tb_name: string | null
    sync_mode: 'whole' | 'time_offset' | null
    interval: 'daily' | 'weekly' | 'monthly' | 'quarterly' | 'yearly' | 'disposable' | null
    column_mappings: Array<{
      src_col_name: string
      src_col_comment: string
      src_col_description: string
      dst_col_name: string
      is_required: boolean
    }>
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${order_id}/data-sync-order-items/${id}`,
    method: 'put',
    data
  })
}

export const getTableTargetFields = (id: string, table_name: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${id}/tables/${table_name}/fields`,
    method: 'get'
  })
}

export const submitTheShoppingOrder = (
  id: string,
  data: {
    name: string
    source: '产业大脑项目' | '产业大脑SAAS' | '金融数据服务项目'
    proj_id: number
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/data-sync/${id}:submit`,
    method: 'post',
    data
  })
}

export const pauseTheOrder = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/data-sync/${id}:pause`,
    method: 'post'
  })
}
export const pauseTheOrderItem = (id: string, order_id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${order_id}/data-sync-order-items/${id}:stop`,
    method: 'post'
  })
}

export const getShoppingOrderInfo = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/data-sync/${id}`,
    method: 'get'
  })
}

export const copyOrdersToCart = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/data-sync/${id}:copy`,
    method: 'post'
  })
}

export const getSpecVals = (params: { cart_item_id?: string; order_item_id?: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/spec-vals',
    method: 'get',
    params: {
      ...params,
      page: 1,
      page_size: 9999
    }
  })
}

export const getSpecs = (data_product_id: string) => {
  return datashopAxios({
    url: '/api/datashop/v3/specifications',
    method: 'get',
    params: {
      data_product_id,
      page: 1,
      page_size: 9999
    }
  })
}

export const getAreaDetail = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/specifications/area-specs/${id}`,
    method: 'get'
  })
}

export const createOrderItem = (data: {
  dst_tb_name: string
  interval: string
  column_mappings: Array<{
    src_col_name: string
    src_col_comment: string
    src_col_description: string
    dst_col_name: string
    is_required: boolean
  }>
  order_id: string
  data_product_id: string
  spec_val_ids: Array<string>
  sync_mode: string
}) => {
  return datashopAxios({
    url: '/api/datashop/v3/data-sync-order-items',
    method: 'post',
    data
  })
}

export const getOrderLog = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${id}/log-record`,
    method: 'get',
    params: {
      page: 1,
      page_size: 9999
    }
  })
}

export const getTaskLogs = (
  id: string,
  params: {
    page_size: number
    page: number
    task_status?: string
    created_at_after?: string
    created_at_before?: string
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/data-sync-order-items/${id}/logs`,
    method: 'get',
    params
  })
}

export const createPgDb = (data: { name: string; host: string; port: string; user: string; password: string; database: string; schema: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/postgresql-configs',
    method: 'post',
    data
  })
}

export const getPgDetailById = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/postgresql-db-configs/${id}`,
    method: 'get'
  })
}

export const getPgAllTablesById = (
  id: string,
  params: {
    keywords?: string
    page: number
    page_size: number
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/postgresql-db-configs/${id}/tables`,
    method: 'get',
    params
  })
}
export const getPgAllFieldsByIdAndTableName = (
  id: string,
  params: {
    table_name: string
    keywords?: string
    page: number
    page_size: number
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/postgresql-db-configs/${id}/columns`,
    method: 'get',
    params
  })
}

export const getPgDbList = (params: { page: number; page_size: number }) => {
  return datashopAxios({
    url: '/api/datashop/v3/db-configs',
    method: 'get',
    params
  })
}

export const deletePgDb = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/db-configs/${id}`,
    method: 'delete'
  })
}

export const createOrderDraft = (data: { product_ids: Array<string> }) => {
  return datashopAxios({
    url: '/api/datashop/v3/orders',
    method: 'post',
    data
  })
}

export const getOrderList = (params: { owner_id?: string; draft: boolean; page: number; page_size: number }) => {
  return datashopAxios({
    url: '/api/datashop/v3/orders',
    method: 'get',
    params
  })
}

export const getOrderDraft = () => {
  return getOrderList({
    page: 1,
    page_size: 10,
    owner_id: getItem(USERRESULT)?.id,
    draft: true
  })
}

export const putOrderDraft = (
  id: string,
  data: {
    name: string
    requirement_source: string
    db_config_id: string | null
    project_id: number | null
    scope_id: string | null
    update_frequency: 'monthly' | 'weekly' | 'daily' | 'yearly' | 'once'
    update_frequency_values: Array<string>
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${id}`,
    method: 'put',
    data
  })
}

export const deleteOrderDraft = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/orders/${id}`,
    method: 'delete'
  })
}

export const getCommonScopeDraft = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/scopes/${id}`,
    method: 'get'
  })
}

export const createCommonScope = (data: { name: string; type: 'specification' | 'company-white-list'; order_id?: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/scopes',
    method: 'post',
    data
  })
}

export const getCommonScopeList = (params: { owner_id?: string; page: number; page_size: number; draft?: boolean }) => {
  return datashopAxios({
    url: '/api/datashop/v3/scopes',
    method: 'get',
    params
  })
}

export const getAllCommonScopeList = (params: { owner_id?: string }) =>
  getCommonScopeList({
    ...params,
    page: 1,
    page_size: 9999
  })

export const putCommonScopeName = (
  id: string,
  data: {
    name: string
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/scopes/${id}`,
    method: 'put',
    data
  })
}

export const deleteCommonScope = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/scopes/${id}`,
    method: 'delete'
  })
}

export const copyCommonScope = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/scopes/${id}:copy-to-draft`,
    method: 'post'
  })
}

export const getSubCommonScopeList = (params: { scope_id: string; page: number; page_size: number }) => {
  return datashopAxios({
    url: '/api/datashop/v3/sub-scopes',
    method: 'get',
    params
  })
}

export const createSubCommonScopeList = (data: { scope_id: string }) => {
  return datashopAxios({
    url: '/api/datashop/v3/sub-scopes',
    method: 'post',
    data
  })
}

export const deleteSubCommonScopeList = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/sub-scopes/${id}`,
    method: 'delete'
  })
}

export const getScopeDetailList = (sub_scope_id: string) => {
  return datashopAxios({
    url: '/api/datashop/v3/scope-items',
    method: 'get',
    params: {
      sub_scope_id,
      page: 1,
      page_size: 100
    }
  })
}

export const getScopeDetailItem = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/scope-items/${id}`,
    method: 'get'
  })
}

export const editScopeDetailItem = (
  id: string,
  data: {
    values: Array<{
      name: string
      value: string
    }>
  }
) => {
  return datashopAxios({
    url: `/api/datashop/v3/scope-items/${id}`,
    method: 'put',
    data
  })
}

export const deleteScopeDetailItem = (id: string) => {
  return datashopAxios({
    url: `/api/datashop/v3/scope-items/${id}`,
    method: 'delete'
  })
}

export const createScopeDetailItem = (data: {
  type: 'pca' | 'park' | 'industry'
  values: Array<{
    name: string
    value: string
  }>
  sub_scope_id: string
}) => {
  return datashopAxios({
    url: '/api/datashop/v3/scope-items',
    method: 'post',
    data
  })
}
